package cc.telecomdigital.tdstock.trading.widget;

import cc.telecomdigital.tdstock.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AppDialogOK extends Dialog implements Button.OnClickListener,
	OnCancelListener
{
	public interface IAppDialogOK
	{
		/** What to do when an OK is pressed **/		
		void OnOK_Click(DialogInterface iDialog);

		/** What to do when user pressed back button **/		
		void OnDialogCancel(DialogInterface iDialog);
	}

	private IAppDialogOK	interfaceHandle;

	/** Create an OK type Dialog box with the imported owner **/
	public AppDialogOK(Context iContext)
	{
		super(iContext, R.style.TradeDialog_Style);
		setContentView(R.layout.app_dialog_ok);
		SetClickableObject();
	}

	protected void SetClickableObject()
	{
		Button buttonObj;

		buttonObj = (Button) findViewById(R.id.app_dialog_ok_OKButton);
		buttonObj.setOnClickListener(this);
		this.setOnCancelListener(this);	
	}	
	
	/** Set the Button Text from resource id; OK is the default **/
	public void SetButtonText(int iStringId)
	{
		((TextView) findViewById(R.id.app_dialog_ok_OKButton))
				.setText(iStringId);
	}

	/** Set the Button Text; OK is the default **/
	public void SetButtonText(String iOKText)
	{
		((TextView) findViewById(R.id.app_dialog_ok_OKButton))
				.setText(iOKText);
	}

	/** Set the Dialog Content **/	
	public void SetTextContent(String iContent)
	{
		TextView textViewObj;

		textViewObj = (TextView) findViewById(R.id.app_dialog_ok_Content);
		
		iContent = iContent.replace("\\n", "\n");
		textViewObj.setText(iContent);
	}

	/** Set the Dialog Content from resource id **/	
	public void SetTextContent(int iStringId)
	{
		String tmpString;

		tmpString = getContext().getResources().getString(iStringId);		
		SetTextContent(tmpString);				
	}

	/** Shows the dialog by putting in the return interface
	    The Dialog will be dismissed once either one of the action is done. **/	
	public void show(IAppDialogOK iInterfaceHandle, boolean iCancelable)
	{
		interfaceHandle = iInterfaceHandle;

		this.setCancelable(iCancelable);
		this.show();
	}

	@Override
	public void onClick(View iViewObj)
	{
		switch (iViewObj.getId())
		{
			case R.id.app_dialog_ok_OKButton:
				this.dismiss();
				if (interfaceHandle != null)
					interfaceHandle.OnOK_Click(this);
				break;
		}
	}

	@Override
	public void onCancel(DialogInterface iDialog)
	{
		// TODO Auto-generated method stub
		iDialog.dismiss();
		if (interfaceHandle != null)
			interfaceHandle.OnDialogCancel(iDialog);
	}
}
